<template>
  <div class="parent">
    <echartsDom v-if="isShowEcharts" style="width: 100%; height: 100%" :width="width" :height="height" :option="option" :type="type" />
    <div v-else>
      <px-empty :image-size="200" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, toRefs, ref, watch } from "vue";
import echartsDom from "./echartsDom.vue";
const props = defineProps({
  width: {
    type: String,
    default: ""
  },
  height: {
    type: String,
    default: ""
  },
  color: {
    type: String,
    default: "#3B82F6"
  },
  type: {
    type: String,
    default: ""
  },
  xlist: {
    type: Object,
    required: true
  },
  chartlist: {
    type: Object,
    required: true
  }
});

const isShowEcharts = ref(false);
const state = reactive({
  option: {},
  yAxis: []
});
const { option, yAxis } = toRefs(state);

const getoptions = () => {
  if (props.chartlist != null) {
    isShowEcharts.value = true;
    yAxis.value = [
      {
        name: "预测",
        type: "line",
        data: props.chartlist.map(item => (item != null ? item.value : 0))
      }
    ];
  } else {
    isShowEcharts.value = false;
  }
  console.log(yAxis.value);
  option.value = {
    toolbox: {
      show: true,
      feature: {
        saveAsImage: {
          title: "点击保存为图片"
        }
      }
    },
    grid: {
      left: "40px",
      top: "80px",
      right: "0px",
      bottom: "10px",
      containLabel: true
    },
    legend: {
      top: "1%",
      type: "scroll",
      width: "1000px"
    },
    tooltip: {
      trigger: "axis",
      confine: true
    },
    xAxis: {
      type: "category",
      boundarGap: false,
      data: props.xlist
    },
    yAxis: {
      name: "%",
      type: "value"
    },
    series: yAxis.value
  };
};
watch(
  () => props.xlist,
  newOption => {
    getoptions();
  },
  {
    immediate: true
  }
);
</script>

<style scoped lang="scss">
.parent {
  position: relative;
  float: left;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.topIcon {
  position: absolute;
  top: 7px;
  right: 8px;
  width: 20px;
  cursor: pointer;
}

.linediv {
  position: relative;
  float: left;
  width: 23px;
  height: 17px;
  margin-right: 10px;

  .line {
    position: absolute;
    top: 8px;
    width: 100%;
    height: 1px;
    background: #3b82f6;
  }

  .yuan {
    position: absolute;
    top: 6px;
    left: 9px;
    width: 5px;
    height: 5px;
    background: #3b82f6;
    border-radius: 50%;
  }
}
</style>
